package com.zzty.springmybatis.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zzty.springmybatis.DAO.UserDAO;
import com.zzty.springmybatis.entity.SelfExam;
import com.zzty.springmybatis.entity.User;
import com.zzty.springmybatis.service.DataBaseSelectService;
import com.zzty.springmybatis.service.SelfExamService;
import com.zzty.springmybatis.service.SelfExamTransationService;
import com.zzty.springmybatis.service.UserService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/selfgread")
public class SelfExamGreadController {
	@Autowired
	UserDAO userDAO;
	@Autowired
	SelfExamService selfExamService;
	@Autowired
	SelfExamTransationService selfExamTransationService;

	@RequestMapping(method = RequestMethod.GET)
	public String testSpring(@ModelAttribute User user, ModelMap model) {
		model.addAttribute("command", user);
		System.out.println("go to SelfExamGread");
		return "SelfExamGread";
	}

	@RequestMapping(value = "/Call", produces = { "text/html;charset=UTF-8;" }, method = RequestMethod.POST)
	@ResponseBody
	public String scoreSearch(HttpServletRequest request, @RequestBody JSONObject json) throws Exception {
		System.out.println("this is call bu date");
		User user = (User) request.getSession().getAttribute("User");
		// String date = json.getString("date");
		Integer clazzId = user.getClazzId();
		// Integer clazzId = 4;

		List<SelfExam> selfExam = selfExamService.searchSelfExamByWinnerIdAndClazzId(clazzId);
		List<String> date = new ArrayList<>();
		for (int i = 0; i < selfExam.size(); i++) {
			date.add(selfExam.get(i).getDate());
			System.out.println(selfExam.get(i).getDate());
		}

		JSONArray jsonArray = new JSONArray();
		List<Integer> userids = userDAO.searchStuIdByclazzId(clazzId);
		for (int j = 0; j < date.size(); j++) {
			for (int i = 0; i < userids.size(); i++) {
				JSONObject json1 = new JSONObject();
				int userId = userids.get(i);
				json1.put("userId", userId);
				json1.put("date", date.get(j));
				User userforselect = new User();
				userforselect.setId(userId);
				User stu = userDAO.selectUserInfo(userforselect);
				json1.put("stuName", stu.getDisname());
				int examId = selfExamService.searchIdByDateAndClass(date.get(j), clazzId);
				// ArrayList questions =
				// selfExamService.searchQstId(date.get(j),
				// Integer.valueOf(clazzId));
				// ArrayList Questions =
				// selfExamService.searchQstContent(questions);
				// String question = "";
				// for (int k = 0; k < Questions.size(); k++) {
				// question += Questions.get(k) + "\r\n";
				// }
				// json1.put("question", question);
				Integer totalTime = selfExamTransationService.searchTotalTimeById(examId, userId);
				if(totalTime==null){
					json1.put("totalTime", "");
				}else{
					json1.put("totalTime", totalTime);
				}
				
				
				String score = selfExamTransationService.searchScore(examId, userId);
				if(score==null){
					json1.put("score", "");
				}else{
					json1.put("score", score);
				}
				
				System.out.println("score"+score);
				Integer valid = selfExamTransationService.searchIsValid(examId, userId);
				json1.put("valid", valid);
				jsonArray.add(json1);
			}
		}
		return jsonArray.toString();
	}

	@RequestMapping(value = "/group", method = RequestMethod.POST)
	@ResponseBody
	public String sotrGroup(HttpServletRequest request, @RequestBody JSONObject json) throws Exception {
		return null;

	}

}
